<template>
  <div class="userEdit">
    <el-dialog title="用户修改" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <span slot="footer" class="dialog-footer">
        <div class="form">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-radio-group v-model="ruleForm.sex">
                <el-radio label="1">男</el-radio>
                <el-radio label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { Message } from 'element-ui'
export default {
  name: 'userEditView',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogInfo: Object

  },
  watch: {
    dialogInfo: {
      handler (newVal, oldVal) {
        this.ruleForm = Object.assign({
          id: newVal.id,
          username: newVal.username,
          password: newVal.password,
          phone: newVal.phone,
          sex: newVal.sex
        })
        // console.log(newVal, oldVal)
      }
    }
  },
  data () {
    return {
      ruleForm: {
        username: '',
        password: '',
        phone: '',
        sex: '' // 性别
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { min: 11, max: 11, message: '号码不合法', trigger: 'blur' }
        ]
        // sex: [
        //   { type: 'array', required: true, message: '请选择性别', trigger: 'change' }
        // ]
      }
    }
  },
  methods: {
    handleClose () {
      this.$emit('update:dialogVisible', false)
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('update:dialogVisible', false)
          this.$emit('update:dialogInfo', this.ruleForm)
          Message({
            message: '修改成功!',
            type: 'success',
            duration: 2 * 1000
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style>
